<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}商品`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="菜品名称" prop="name">
        <el-input v-model="drawerProps.row.name" clearable></el-input>
      </el-form-item>
      <el-form-item label="菜品价格" prop="price">
        <el-input v-model="drawerProps.row.price" clearable></el-input>
      </el-form-item>
      <el-form-item label="菜品分类" prop="categoryId">
        <el-select v-model="drawerProps.row.categoryId" clearable>
          <template v-for="(categoryItem, categoryIndex) in categoryList" :key="categoryIndex">
            <el-option :value="categoryItem.id" :label="categoryItem.name" />
          </template>
        </el-select>
      </el-form-item>
      <el-form-item label="是否上架" prop="availability">
        <el-switch v-model="drawerProps.row.availability" :active-value="1" :inactive-value="0" />
      </el-form-item>
      <el-form-item label="菜品描述" prop="description">
        <el-input type="textarea" v-model="drawerProps.row.description" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>
<script setup lang="ts">
import { ref, reactive } from "vue";
import { ElMessage } from "element-plus";

const categoryList = ref<Array<any>>([
  {
    id: 1,
    name: "推荐"
  },
  {
    id: 2,
    name: "荤菜"
  },
  {
    id: 3,
    name: "素菜"
  },
  {
    id: 4,
    name: "饮品"
  },
  {
    id: 5,
    name: "酒水"
  },
  {
    id: 6,
    name: "甜品"
  },
  {
    id: 7,
    name: "主食"
  },
  {
    id: 8,
    name: "其他"
  }
]);

const rules = reactive({
  name: [{ required: true, message: "请输入菜品名称" }],
  price: [{ required: true, message: "请输入菜品价格" }],
  categoryId: [{ required: true, message: "请选择菜品分类" }],
  description: [{ required: true, message: "请输入菜品描述" }]
});

interface DrawerProps {
  title: string;
  isView: boolean;
  row: any;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}
const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

const ruleFormRef = ref<any>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      const params = {
        ...drawerProps.value.row,
        price: drawerProps.value.row.price * 100
      };
      await drawerProps.value.api!(params);
      ElMessage.success({ message: `${drawerProps.value.title}菜品成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
